package de.pyranja.money.exception;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Currency;
import java.util.List;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import de.pyranja.money.core.BaseMoney;


/**
 * MoneyException indicating an attempt to operate on money amounts with 
 * different, non-compatible currencies.
 * 
 * @author Chris Borckholder
 */
@SuppressWarnings("serial")
public class CurrencyMismatchException extends MoneyException {
	
	// message strings
	private static final String MESSAGE_INTRO = "Currency Mismatch :";
	private static final String LIST_INTRO = "\nMismatched currencies :";
	// join string arrays
	private static final Joiner JOINER = Joiner.on(",").useForNull("null"); 
	
	private final Collection<Currency> mismatched;
	
	public CurrencyMismatchException(String message, BaseMoney... causes) {
		super(MESSAGE_INTRO + message);
		this.mismatched = new ArrayList<Currency>(causes.length);
		for (BaseMoney current : causes) {
			this.mismatched.add(current.getCurrency());
		}
	}
	
	public CurrencyMismatchException(String message, Currency... mismatched) {
		// Construct message
		super(MESSAGE_INTRO + message);
		this.mismatched = Arrays.asList(mismatched);
	}
	
	public CurrencyMismatchException(String message) {
		this(message, new Currency[] { } );
	}
	
	public List<Currency> getMismatchedCurrencies() {
		return ImmutableList.copyOf(mismatched);
	}
	
	@Override
	public String getMessage() {
		StringBuilder sb = new StringBuilder();
		sb.append(super.getMessage());
		sb.append(LIST_INTRO);
		sb.append(JOINER.join(mismatched,';'));
		return sb.toString();
	}
}
